package components;

import game.Main;
import components.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

public class Map implements Serializable{ 
	private static final long serialVersionUID = 2L;
/**
 * This Object includes the information about the played map. 
 * Informations about size, fields and Start positions are defined here. 
 * It also loads all Information out of the file an convert them in the needed format.
 */
	/**
	 * This is the reference to the .txt file, which contains all information
	 */
	public File source;

	/**
	 * The size of the field
	 */
	public int size;


	/**
	 * This array contains all used fields
	 */
	public  Field[][] field;

	/**
	 * This array contains all used ground
	 */
	public  Field[][] ground;

	/**
	 * This array contains all starting points
	 */
	private float[][] starts;

	/**
	 * The size of a single block depends on the total amount of blocks and the size of the window and this result is saved here
	 */
	public int blocksize;

	/**
	 * This is the size in pixel of the whole field.
	 */
	public int sizefield;

	/**
	 * This is the reference to all playing figures
	 */
	public  Bomberman[] bomber;

	/**
	 * The standard Constructor which creates field and player
	 * @param file The file with all the informations
	 * @param life The amount of lifes which should be used to initialize the player
	 * @param playing The number of player
	 */
	public Map(File file, int life, int playing){
		try {
			this.source=file;
			String[] help;
			String[][] help2;
			BufferedReader br = new BufferedReader(new FileReader(this.source));
			String row = null;

			row = br.readLine();
			this.size=Integer.parseInt(row);

			//this.blocksize=Main.sizefield/this.size;
			this.blocksize=30;
			this.sizefield=this.blocksize*this.size;

			row = br.readLine();
			this.starts = new float[Integer.parseInt(row)][2];
			this.bomber = new Bomberman[playing];
			help2=new String[Integer.parseInt(row)][2];

			row=br.readLine();
			help=row.split(";");
			for(int i=0;i<help2.length;i++){
				help2[i]=help[i].split(",");
			}
			for(int i=0;i<help2.length;i++){
				for(int j=0;j<help2[i].length;j++){
					this.starts[i][j]=Integer.parseInt(help2[i][j]);
				}
			}

			for(int i=0;i<playing;i++){
				this.bomber[i]=new Bomberman(this.starts[i], i, life);
			}

			this.field=new Field[this.size][this.size];
			for(int i =0;i<this.size;i++){
				row=br.readLine();
				help=row.split(",");
				for(int j=0;j<this.size;j++){
					if(Integer.parseInt(help[j])==0){
						this.field[i][j]=new Block(new float[]{j,i},false,false,false,game.Multiplayer.fields[Integer.parseInt(help[j])]);
					}
					else if(Integer.parseInt(help[j])==1){
						this.field[i][j]=new Space(new float[]{j,i});
					}
					else{
						this.field[i][j]=new Block(new float[]{j,i},true,false,false,game.Multiplayer.fields[Integer.parseInt(help[j])]);
					}
				}
			}

			br.readLine();

			this.ground=new Field[this.size][this.size];
			for(int i =0;i<this.size;i++){
				row=br.readLine();
				help=row.split(",");
				for(int j=0;j<this.size;j++){
					this.ground[i][j]=new Block(new float[]{i,j},false,false,true,game.Multiplayer.grounds[Integer.parseInt(help[j])]);
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * Sets the field back to its original status
	 */
	public void setFieldBack(){

		try {
			BufferedReader br = new BufferedReader(new FileReader(this.source));
			String[] help;
			//String[][] help2;
			String row = null;
			row = br.readLine();
			row = br.readLine();
			row = br.readLine();

			this.field=new Field[this.size][this.size];
			for(int i =0;i<this.size;i++){
				row=br.readLine();
				help=row.split(",");
				for(int j=0;j<this.size;j++){
					if(Integer.parseInt(help[j])==0){
						this.field[i][j]=new Block(new float[]{j,i},false,false,false,game.Multiplayer.fields[Integer.parseInt(help[j])]);
					}
					else if(Integer.parseInt(help[j])==1){
						this.field[i][j]=new Space(new float[]{j,i});
					}
					else{
						this.field[i][j]=new Block(new float[]{j,i},true,false,false,game.Multiplayer.fields[Integer.parseInt(help[j])]);
					}
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String row = null;
	}

	/**
	 * Method to get the start position to one player
	 * @param i ID of the player
	 * @return Returns a float array with the start coordinates
	 */
	public float[] getStarts(int i) {
		return starts[i];
	}
	
	/**
	 * Method to get all start positions
	 * @return Returns a float array with all start positions
	 */
	public float[][] getStarts() {
		return starts;
	}
}
